<?php
// +----------------------------------------------------------------------
// | Description: 系统用户
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\logic\AdminLogic;
use app\common\controller\AdminAuthController;
use app\common\event\AdminLogin;
use app\common\token\token;
use app\common\validate\AdminValidate;
use hg\apidoc\annotation as Apidoc;
use think\App;

/**
 * 后台管理员
 * Class Admin
 * @package app\admin\controller
 * @Apidoc\Title("后台管理员")
 */
class Admin extends AdminAuthController
{

    /**
     * 构造器
     * Admin constructor.
     *
     * @param App|null $app
     */
    public function __construct(App $app = NULL)
    {
        parent::__construct($app);
        $this->validate = AdminValidate::instance();
        $this->logic = AdminLogic::instance();
    }

    /**
     * 获取后台管理员详情(带菜单)
     * @return \think\response\Json
     * @throws \app\common\exception\TokenException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {

        $user = Token::instance()->getUserInfo();
        $uid = $user[$user->getPk()];
        $data = $this->logic->getMore($uid);
        $data['userInfo']->hidden(['groups']);
        event(AdminLogin::class, $uid);
        return $this->json_responce($data);
    }

    /**
     * 刷新
     * @return \think\response\Json
     * @throws \app\common\exception\TokenException
     */
    public function refresh()
    {

        $user = Token::instance()->getUserInfo();
        $uid = $user[$user->getPk()];
        $data['token'] = $this->logic->createJwt($user);
        $data['expire'] = $_SERVER['REQUEST_TIME'] + 60 * 60 * 24 * 30;
        event(AdminLogin::class, $uid);
        return $this->json_responce($data);
    }

    /**
     * @Apidoc\Title("获取管理员列表")
     * @Apidoc\Desc("获取管理员列表")
     * @Apidoc\Url("/admin/admin/index")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("admin")
     * @Apidoc\Header("admin-token", require=true, desc="Token")
     * @Apidoc\Returned("count",type="int",default="0",desc="主键")
     * @Apidoc\Returned("list",type="array",desc="列表",
     *     @Apidoc\Returned ("0",type="object",desc="所属组",
     *          @Apidoc\Returned("admin_id",type="int",default="0",desc="主键"),
     *          @Apidoc\Returned("username",type="varchar",default="管理后台账号",desc="管理后台账号"),
     *          @Apidoc\Returned("phone",type="varchar",default="手机",desc="手机"),
     *          @Apidoc\Returned("password",type="varchar",default="管理后台密码",desc="管理后台密码"),
     *          @Apidoc\Returned("remark",type="varchar",default="用户备注",desc="用户备注"),
     *          @Apidoc\Returned("create_time",type="bigint",default="0",desc="create_time"),
     *          @Apidoc\Returned("update_time",type="bigint",default="0",desc="update_time"),
     *          @Apidoc\Returned("realname",type="varchar",default="真实姓名",desc="真实姓名"),
     *          @Apidoc\Returned("structure_id",type="int",default="0",desc="部门"),
     *          @Apidoc\Returned("post_id",type="int",default="0",desc="岗位"),
     *          @Apidoc\Returned("status",type="tinyint",default="0",desc="状态,1启用0禁用"),
     *          @Apidoc\Returned("delete_time",type="bigint",default="0",desc="delete_time"),
     *          @Apidoc\Returned("creater_id",type="int",default="0",desc="创建者"),
     *          @Apidoc\Returned("updater_id",type="int",default="0",desc="修改者"),
     *     )
     * )
     */
    public function index()
    {
        $get = input_param([
            'page' => 1,
            'keywords' => '',
            'limit' => 20,
            'group_id' => '',
            'structure_id' => '',
            'post_id' => '',
        ]);
        $this->validate->runCheck($get, '', __FUNCTION__);
        $data['list'] = $this->logic->getAdminList($get);
        $data['count'] = $this->logic->getAdminCount($get);
        return $this->json_responce($data);
    }

    /**
     * @Apidoc\Title("获取管理员详情")
     * @Apidoc\Desc("获取管理员详情")
     * @Apidoc\Url("/admin/admin/read")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("admin")
     * @Apidoc\Header("admin-token", require=true, desc="Token")
     * @Apidoc\Param("id", type="integer|length:1,10",require=true, default="1", desc="ID")
     * @Apidoc\Returned("info",type="object",desc="信息",
     *    @Apidoc\Returned("admin_id",type="int",default="0",desc="主键"),
     *    @Apidoc\Returned("username",type="varchar",default="管理后台账号",desc="管理后台账号"),
     *    @Apidoc\Returned("phone",type="varchar",default="手机",desc="手机"),
     *    @Apidoc\Returned("password",type="varchar",default="管理后台密码",desc="管理后台密码"),
     *    @Apidoc\Returned("remark",type="varchar",default="用户备注",desc="用户备注"),
     *    @Apidoc\Returned("create_time",type="bigint",default="0",desc="create_time"),
     *    @Apidoc\Returned("update_time",type="bigint",default="0",desc="update_time"),
     *    @Apidoc\Returned("realname",type="varchar",default="真实姓名",desc="真实姓名"),
     *    @Apidoc\Returned("structure_id",type="int",default="0",desc="部门"),
     *    @Apidoc\Returned("post_id",type="int",default="0",desc="岗位"),
     *    @Apidoc\Returned("status",type="tinyint",default="0",desc="状态,1启用0禁用"),
     *    @Apidoc\Returned("delete_time",type="bigint",default="0",desc="delete_time"),
     *    @Apidoc\Returned("creater_id",type="int",default="0",desc="创建者"),
     *    @Apidoc\Returned("updater_id",type="int",default="0",desc="修改者"),
     * )
     */
    public function read()
    {
        $get = input_param([
            'id' => '',
        ]);
        $this->validate->setAnnotateChekck(static::class, __FUNCTION__)
                       ->runCheck($get, '', __FUNCTION__);
        $data['info'] = $this->logic
            ->read($get['id'])
            ->toArray();
        return $this->json_responce($data);
    }

    /**
     * @Apidoc\Title("新增管理员")
     * @Apidoc\Desc("新增管理员")
     * @Apidoc\Url("/admin/admin/save")
     * @Apidoc\Method("post")
     * @Apidoc\Tag("admin")
     * @Apidoc\Header("admin-token", require=true, desc="Token")
     * @Apidoc\Param("realname", type="string",require=true, default="真实姓名", desc="真实姓名")
     * @Apidoc\Param("username", type="string",require=true, default="用户名", desc="用户名")
     * @Apidoc\Param("password", type="string",require=true, default="password", desc="密码")
     * @Apidoc\Param("remark", type="string",require=false, default="remark", desc="备注")
     * @Apidoc\Param("groups", type="array",require=true, default="[1,2,3]", desc="用户组")
     * @Apidoc\Param("structure_id", type="integer",require=true, default="1", desc="部门")
     */
    public function save()
    {
        $param = $this->param;
        $this->validate->setAnnotateChekck(static::class, __FUNCTION__)
                       ->runCheck($param, '', __FUNCTION__);
        $data = $this->logic->createData($param);
        return $this->json_responce();
    }

    public function update()
    {

        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->logic->updateByID($param);
        return $this->json_responce();
    }

    /**
     * 后台管理员单条删除
     * @return \think\response\Json
     */
    public function delete()
    {

        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->logic->delByID($param);
        return $this->json_responce();
    }

    /**
     * 后台管理员批量删除
     * @return \think\response\Json
     */
    public function deletes()
    {

        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->logic->delByIDs($param);
        return $this->json_responce();
    }

    /**
     * 后台管理员启用/禁用
     * @return \think\response\Json
     */
    public function enables()
    {

        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->logic->enableByIDs($param);
        return $this->json_responce();
    }

}
